package com.zhouyu;

import com.zhouyu.service.OrderService;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;

public class ProxyTest {

	public static void main(String[] args) {

		OrderService orderService = new OrderService();

		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(orderService);
//		proxyFactory.addInterface(OrderInterface.class);
//		proxyFactory.setOptimize(true);
		proxyFactory.addAdvice(new MethodBeforeAdvice() {
			@Override
			public void before(Method method, Object[] args, Object target) throws Throwable
			{
				System.out.println("方法执行之前");
			}
		});

		proxyFactory.addAdvice(new AfterReturningAdvice() {
			@Override
			public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
			{
				System.out.println("方法执行之后");
			}
		});

		proxyFactory.addAdvice(new MethodInterceptor() {
			@Nullable
			@Override
			public Object invoke(@NotNull MethodInvocation invocation) throws Throwable
			{
				System.out.println("Around 前");
				Object proceed = invocation.proceed();
				System.out.println("Around 后");
				return proceed;
			}
		});

		OrderService proxy = (OrderService)proxyFactory.getProxy();
		proxy.test();
	}
}